package UiUtil;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;


public class BrowserSetup {
	
	
	public WebDriver driver;
	public WebElement element,eventElement;
	PrintWriter log = null;
	File directory = new File (".");
	public BrowserSetup(String DriverPath,String BrowserName,String Url)
	{
		if(BrowserName.equalsIgnoreCase("Chrome") && DriverPath!=null)
		{
			this.driver = getChrome(DriverPath);
			launchUrl(Url);
			
		}
		
			
		
	}
	public String launchStep(String framexpath,String eventid,String eventname,String eventxpath,String action,String actionvalue)
	{ int flag=0;
		try{
			if(action.equalsIgnoreCase("getinfo"))
			{
				if(ReadTestCase.PageValidationTitle==null)
				{
					System.out.print("Click the Event to get info about that event");
				}
				else{

					try {
						if(!new File(directory.getCanonicalPath()+"\\logs").isDirectory())
						{
							new File(directory.getCanonicalPath()+"\\logs").mkdirs();
						}
						log = new PrintWriter(new File(directory.getCanonicalPath()+"\\logs\\"+ReadTestCase.PageValidationTitle+new Date().toString().replaceAll(":", "-")+".properties"));
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					writegetInfo();
						
				}
			}
			else
			{
			waittoload();
			if(framexpath!=null && framexpath!="" && framexpath!=" ")
			{
				launchPageByXpath(framexpath);
			}
			if(eventid!=null  && eventid!="" && eventid!=" ")
			{
				launchEventById(eventid);
				flag=1;
			}
			else if(eventname!=null  && eventname!="" && eventname!=" ")
			{
				launchEventByName(eventname);	
				flag=2;
			}
			else  if(eventxpath!=null  && eventxpath!="" && eventxpath!=" ")
			{
				launchEventByXpath(eventxpath);	
				flag=3;
			}
			waittoload();
			if(action.equalsIgnoreCase("click"))
			{
				clickElement(eventElement);
			}
			else if(action.equalsIgnoreCase("sendKey"))
			{
				sendElementValue(eventElement,actionvalue);
			}
			else
			{
				System.out.println("invalid action");
				return "failed";
			}
			}
			//exitTestcase(this.driver);
		}
		catch(Exception e)
		{
			
			return e.getMessage();
		}
		return "Passed";
		
	}
	public void exitTestcase()
	{
		this.driver.quit();
	}
	
	public WebDriver getChrome(String path)
	{
		System.setProperty("webdriver.chrome.driver", path);
		WebDriver cdriver = new ChromeDriver();
		
		return cdriver;
	}
	
	public void launchUrl(String Url)
	{
		driver.get(Url);
		waittoload();
		
	}
	public void launchEventById(String Id)
	{
		
		this.eventElement=getElementById(Id);
		
	}
	public void launchEventByName(String Name)
	{
		
		this.eventElement=getElementByName(Name);
		
	}
	public void launchEventByXpath(String Xpath)
	{
		
		this.eventElement=getElementByXpath(Xpath);
		
	}
	public void launchPageById(String Id)
	{
		switchDefaultElement();
		this.element=getElementById(Id);
		switchElement(this.element);
	}
	public void launchPageByName(String Name)
	{
		switchDefaultElement();
		this.element=getElementByName(Name);
		switchElement(this.element);
	}
	public void launchPageByXpath(String Xpath)
	{
		switchDefaultElement();
		this.element=getElementByXpath(Xpath);
		switchElement(this.element);		
	}
	public void waittoload()
	{
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	}
	public void switchElement(WebElement switchElement)
	{
		driver.switchTo().frame(switchElement);
	}
	public void switchDefaultElement()
	{		
		driver.switchTo().defaultContent();
	}
	
	public WebElement getElementById(String id)
	{
		element = driver.findElement(By.id(id));
		return element;		
	}
	public WebElement getElementByName(String name)
	{
		element = driver.findElement(By.name(name));
		return element;		
	}
	public WebElement getElementByXpath(String xpath)
	{
		element = driver.findElement(By.xpath(xpath));
		return element;		
	}
	public void clickElement(WebElement clickElement)
	{
		clickElement.click();
		waittoload();
	}
	public void submitElement(WebElement submitElement)
	{
		submitElement.submit();
		waittoload();
	}
	public void sendElementValue(WebElement sendElement,String value )
	{
		sendElement.sendKeys(value);
	}
	public void clearElementValue(WebElement clearElement)
	{
		clearElement.clear();
	}
	public void writegetInfo()
	{
		List<WebElement> outputText2= driver.findElements(By.className("outputText"));  
		//System.out.println(input.size());
		
		for(int i=0;i<outputText2.size();i++)
		{

			// System.out.println(input.get(i).getAttribute("id"));
			if(outputText2.get(i).getAttribute("style").isEmpty() )
			{

				if(!outputText2.get(i).getAttribute("id").isEmpty())
				{
				log.print(outputText2.get(i).getAttribute("id")+"=");
				log.println(outputText2.get(i).getText());
				}

			}

		}
		
		log.close();
	}
	public static void main(String[]a)
	{
		//BrowserSetup br= new BrowserSetup(a[0], a[1], a[2], a[3],a[4]);
	}

}
